<?php

class CatalogController extends Controller
{
	public $layout='//layouts/catalog';

	public function filters()
	{
		return array(
			'accessControl',
		);
	}
        
	public function accessRules()
	{
		return array(
			array('allow',
				'actions'=>array('index','view','search','updatefamilysubcategoriesajax'),//TODO controlar metodo Sarch q no se puedan agregar parametros por url.
				'users'=>array('*'),
			),
			array('allow',
				'actions'=>array(''),
				'users'=>array('@'),
			),
			array('allow',
				'actions'=>array('create','update','admin','delete'),
				'users'=>array('admin'),
			),
			array('deny',
				'users'=>array('*'),
			),
		);
	}

	public function actionView($id)
	{
		$this->render('view',array(
			'model'=>$this->loadModel($id),
		));
	}

	public function actionCreate()
	{
		$model=new TanaiProductMaster;

		if(isset($_POST['TanaiProductMaster']))
		{
			$model->attributes=$_POST['TanaiProductMaster'];
			if($model->save())
				$this->redirect(array('view','id'=>$model->ref));
		}

		$this->render('create',array(
			'model'=>$model,
		));
	}

	public function actionUpdate($id)
	{
		$model=$this->loadModel($id);

		if(isset($_POST['TanaiProductMaster']))
		{
			$model->attributes=$_POST['TanaiProductMaster'];
			if($model->save())
				$this->redirect(array('view','id'=>$model->ref));
		}

		$this->render('update',array(
			'model'=>$model,
		));
	}

	public function actionDelete($id)
	{
		if(Yii::app()->request->isPostRequest)
		{
			$this->loadModel($id)->delete();

			if(!isset($_GET['ajax']))
				$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
		}
		else
			throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
	}

	public function actionIndex()
	{       
                $data = array();
                $data["myValue"] = "Content loaded";

                $dataProvider = new CActiveDataProvider('TanaiProductMaster',
                array(
                    'criteria'=>array(
                        'order'=>'entry_date DESC',
                        'limit' => 50,
                    ),
                    'pagination'=>false,
                ));
                
                $this->render('index',array(
			'dataProvider'=>$dataProvider,
                        $data,
		));
	}
        
	public function actionSearch()
	{       
                $id_category_1 = $_GET[id_category_1];
                $id_category_2 = $_GET[id_category_2];
                $id_subcategory = $_GET[id_subcategory];
                if($id_category_1 == NULL){//TODO Hacerlo para que funcione con mas de 2 categorias activadas!
                    $cat = Category::model()->findbypk($id_category_2);
                }else{
                    $cat = Category::model()->findbypk($id_category_1);
                }
                $subcat = Subcategory::model()->findbypk($id_subcategory);
                $data = array();
                $data["myValue"] = "Content loaded";
                if($id_subcategory == ''){
                    $dataProvider=new CActiveDataProvider('TanaiProductMaster',array(
                        'criteria'=>array(
                            'condition'=>'collection = :cat',
                            'params'=>array(':cat'=>$cat->tag),
                        ),
                        'pagination'=>array(
                            'pageSize'=>36,
                        ),
                    ));
                } else {
                    $dataProvider=new CActiveDataProvider('TanaiProductMaster',array(
                        'criteria'=>array(
                            'condition'=>'collection = :cat AND collection_sub = :subcat',
                            'params'=>array(':cat'=>$cat->tag,':subcat'=>$subcat->tag),
                        ),
                        'pagination'=>array(
                            'pageSize'=>36,
                        ),
                    ));
                }
                      
                $this->render('index',array(
			'dataProvider'=>$dataProvider,
                        $data,
                        'fil_category'=>$cat->tag,
                        'fil_subcategory'=>$subcat->tag
		));
	}

	public function actionAdmin()
	{
		$model=new TanaiProductMaster('search');
		$model->unsetAttributes();  // clear any default values
		if(isset($_GET['TanaiProductMaster']))
			$model->attributes=$_GET['TanaiProductMaster'];

		$this->render('admin',array(
			'model'=>$model,
		));
	}

	public function loadModel($id)
	{
		$model=TanaiProductMaster::model()->findByPk($id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}

	protected function performAjaxValidation($model)
	{
		if(isset($_POST['ajax']) && $_POST['ajax']==='tanai-product-master-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}
        
        public function actionUpdateFamilySubcategoriesAjax()
        {
            //$data = array();
            //$data["myValue"] = "Mensaje para la vista";
            //$msgHTML1 = $this->renderPartial('_ajaxListCategories', $data, false, true);
            //$msgHTML = $this->renderInternal('_items', array('model'=>$model), true); //usar este
            $this->renderPartial('_ajaxListFamilySubcategories', $data, false, true);
        }
}